package org.example.api.transform;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.example.api.bean.SensorReading;

/**
 * @author huangqihan
 * @date 2021/2/20
 */
public class TransformByPartition {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        DataStreamSource<String> inputStream = env.readTextFile("src/main/resources/sensor.txt");

        // input
//        inputStream.print("input");

        // shuffle
//        inputStream.shuffle().print("shuffle");

        SingleOutputStreamOperator<SensorReading> mapStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });

        // keyBy
        mapStream.keyBy("id").print("keyBy");

        // global
//        mapStream.global().print("global");

        env.execute();
    }

}
